package com.ocom.accesservice.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.vo.access.PersonnelAreaVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoPersonnelArea extends BaseMapper<PersonnelAreaVo> {

    @Select({"<script>",
            "select \n" +
                    "p.p_id , p_code , p_name  , dept_id , g.group_no\n" +
                    ",g.com_id \n" +
                    "FROM personnel_info p \n" +
                    "left join mj_group_per g on p.p_id = g.p_id and g.area_id =#{areaNo}  \n" +
                    "where p.com_id =#{comId} and p.status = 0  "+
                    " <when test=\"pName!=null and pName!=''\" > "+
                         " AND p.p_name like concat('%', #{pName}, '%') "+
                    " </when> "+
                    " <when test=\"pCode!=null and pCode!=''\" > "+
                        " AND p.p_code =#{pCode}"+
                    " </when> "+
                    " <when test=\"access!=null and access==0\" > "+
                       " and IFNULL(g.group_no,0) = 0"+
                    " </when> "+
                    " <when test=\"deptIds.size()>0 \" > "+
                        "and p.dept_id in" +
                        "<foreach collection='deptIds' item='item' open='(' separator=',' close=')'>" +
                            "#{item}" +
                        "</foreach>"+
                    " </when> "+
                    "</script>"})
    List<PersonnelAreaVo> getAreaAccessListBu(@Param("comId")Long comId,@Param("areaNo") Integer areaNo,
                                              @Param("deptIds")List<Integer> deptIds,@Param("pName") String pName,
                                              @Param("pCode") String pCode,@Param("access") Integer access);

    @Select({"<script>",
            "select \n" +
                    "p.p_id , p_code , p_name , dept_id , g.group_no \n" +
                    "FROM personnel_info p , mj_group_per g \n" +
                    "where p.p_id = g.p_id and g.area_id =#{areaNo}  and p.com_id =#{comId}  and p.status = 0"+
                    " <when test=\"pName!=null and pName!=''\" > "+
                    " AND p.p_name like concat('%', #{pName}, '%') "+
                    " </when> "+
                    " <when test=\"pCode!=null and pCode!=''\" > "+
                    " AND p.p_code =#{pCode}"+
                    " </when> "+
                    " <when test=\"access!=null and access > 0\" > "+
                    " AND g.group_no = #{access}"+
                    " </when> "+
                    " <when test=\"deptIds.size()>0 \" > "+
                    "AND p.dept_id in" +
                    "<foreach collection='deptIds' item='item' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>"+
                    " </when> "+
                    "</script>"})
    List<PersonnelAreaVo> getAreaAccessList(@Param("comId")Long comId,@Param("areaNo") Integer areaNo,
                                            @Param("deptIds")List<Integer> deptIds,@Param("pName") String pName,
                                            @Param("pCode") String pCode,@Param("access") Integer access);
}
